package work.managercenter;

import java.io.File;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;

import utils.MyUtils;

public class ReviewImageUploader {

	public static void main(String[] args) {
		try {
			testServicesReviewImageUpload();

		} catch (Exception e) {
			e.printStackTrace(System.out);
		}
	}

	private static void testServicesReviewImageUpload() throws Exception {
		String url = "http://stg-aptrest3.hq.uw1.ibcloud.us:8080/uploadReviewPhoto.htm";
		String complexId = "9199332346275144326";
		String verificationId = "64543295";

		File fileToUpload = new File("/home/gmarcone/Imágenes/Captura de pantalla de 2014-11-04 10:54:33.png");

		Map<String,String> parameters = new HashMap<String,String>();
		parameters.put("complexId", complexId);
		parameters.put("verificationId", verificationId);

		System.out.println(upload(url,parameters,fileToUpload));
	}

	private static String upload(String url, Map<String,String> parameters,File fileToUpload) throws Exception {

		MultipartEntity entity = new MultipartEntity();

		for (Entry<String, String> entry : parameters.entrySet()) {
			entity.addPart(entry.getKey(), new StringBody(entry.getValue()));
		}

		entity.addPart("file", new FileBody(fileToUpload));

		HttpPost request = new HttpPost(url);
		request.setEntity(entity);

		HttpClient client = new DefaultHttpClient();
		HttpResponse response = client.execute(request);

		return MyUtils.toString(response.getEntity().getContent(), "UTF-8");
	}

}


